package com.mouliu.base.concurrent17;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * CallerRunsPolicy测试
 *
 * @author mouliu
 * @create 2018-04-15-上午1:08
 */
public class CallerRunsPolicyDemo {
    public static void main(String[] args){
        // 创建线程池。线程池的"最大池大小"和"核心池大小"都为1，"线程池"的阻塞队列容量为1。
        ThreadPoolExecutor poolExecutor=
                new ThreadPoolExecutor(1, 1, 0,
                        TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));

        //设置线程池的拒绝策略为CallerRunsPolicy
        poolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        //新建10个任务 并将它们添加到线程池中
        for (int i=0;i<10;i++){
            Runnable myTask = new MyTask(i,"name is"+i);
            poolExecutor.execute(myTask);
        }
        //关闭线程池
        poolExecutor.shutdown();
    }
}
